/**
 * 数据库抽象基类
 * 定义统一的数据库操作接口，支持 SQLite 和 MongoDB
 */

export interface DatabaseConfig {
  type: 'sqlite' | 'mongodb';
  connectionString?: string;
  options?: any;
}

export interface QueryResult<T = any> {
  success: boolean;
  data?: T;
  error?: string;
  count?: number;
}

export interface QueryOptions {
  page?: number;
  pageSize?: number;
  sortBy?: string;
  sortOrder?: 'asc' | 'desc';
  filter?: Record<string, any>;
}

export abstract class BaseDatabase {
  protected config: DatabaseConfig;
  protected connected: boolean = false;

  constructor(config: DatabaseConfig) {
    this.config = config;
  }

  abstract connect(): Promise<void>;
  abstract disconnect(): Promise<void>;
  abstract isConnected(): boolean;

  // CRUD操作
  abstract create<T>(collection: string, data: Partial<T>): Promise<QueryResult<T>>;
  abstract findOne<T>(collection: string, filter: Record<string, any>): Promise<QueryResult<T>>;
  abstract findMany<T>(
    collection: string, 
    filter?: Record<string, any>, 
    options?: QueryOptions
  ): Promise<QueryResult<T[]>>;
  abstract update<T>(
    collection: string, 
    filter: Record<string, any>, 
    data: Partial<T>
  ): Promise<QueryResult<T>>;
  abstract delete(collection: string, filter: Record<string, any>): Promise<QueryResult<boolean>>;
  abstract count(collection: string, filter?: Record<string, any>): Promise<QueryResult<number>>;

  // 批量操作
  abstract createMany<T>(collection: string, data: Partial<T>[]): Promise<QueryResult<T[]>>;
  abstract updateMany<T>(
    collection: string, 
    filter: Record<string, any>, 
    data: Partial<T>
  ): Promise<QueryResult<number>>;
  abstract deleteMany(collection: string, filter: Record<string, any>): Promise<QueryResult<number>>;

  // 数据库特定操作
  abstract createIndex(collection: string, fields: Record<string, any>): Promise<QueryResult<boolean>>;
  abstract dropCollection(collection: string): Promise<QueryResult<boolean>>;
  abstract listCollections(): Promise<QueryResult<string[]>>;
}